package com.ph.shopping.facade.order.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import cm.ph.shopping.facade.order.dto.ManagerOrderDTO;
import cm.ph.shopping.facade.order.entity.PhManagerOrder;
import cm.ph.shopping.facade.order.entity.PhPurchaseList;
import cm.ph.shopping.facade.order.service.IManagerOrderService;
import cm.ph.shopping.facade.order.vo.QueryManagerOrderVo;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ph.shopping.common.core.base.BaseService;
import com.ph.shopping.common.core.customenum.ResultEnum;
import com.ph.shopping.common.core.customenum.RoleEnum;
import com.ph.shopping.common.util.com.order.Constants;
import com.ph.shopping.common.util.com.order.CreateOrderUtil;
import com.ph.shopping.common.util.result.Result;
import com.ph.shopping.common.util.transform.MoneyTransUtil;
import com.ph.shopping.facade.mapper.IManagerOrderMapper;
import com.ph.shopping.facade.mapper.IPurchaseListMapper;

@Component
@Service(version = "1.0.0")
public class ManagerOrderServiceImpl extends BaseService implements IManagerOrderService {

	@Autowired
	private IPurchaseListMapper iPurchaseListMapper;

	@Autowired
	private IManagerOrderMapper iManagerOrderMapper;

	/**
	 * 新增代理商进货单
	 */
	@Override
	public int insertPurchase(List<PhPurchaseList> purchaseList) throws Exception {
		List<Long> productIds = new ArrayList<Long>();
		// 获取商品id集合
		for (PhPurchaseList list : purchaseList) {
			productIds.add(list.getProductId());
		}
		// 查询供应商Id集合
		List<Long> supplerIds = new ArrayList<Long>();
		supplerIds = iPurchaseListMapper.selectSupplers(productIds);

		for (PhPurchaseList purchase : purchaseList) {
			for (Long supplerId : supplerIds) {
				purchase.setPrice(MoneyTransUtil.transMultiDouble(purchase.getPrice1()));
				purchase.setSupplyerId(supplerId);
				int ret = iPurchaseListMapper.insertPurchase(purchase);
				if (ret == 1) {
					if (updateProductNum(purchase.getNum(), purchase.getProductId()) != 1) {
						throw new Exception("更新商品数量失败！");
					}
				} else {
					throw new Exception("新增进货单失败!");
				}
			}
		}
		return Constants.SUCCESS;// 成功
	}

	/**
	 * 
	 * @Title: updateProductNum
	 * @Description: 更新商品数量
	 * @author WQiang
	 * @date 2017年3月18日 下午5:14:42
	 * @return
	 */
	int updateProductNum(int num, long productId) throws Exception {
		return iPurchaseListMapper.updateProductNum(num, productId);
	}

	/**
	 * 
	 * @Title: insertManagerOrder
	 * @Description: 新增进货订单
	 * @author WQiang
	 * @date 2017年3月18日 下午6:52:50
	 * @param managerOrder
	 * @return
	 * @throws Exception
	 */
	public int insertManagerOrder(PhManagerOrder managerOrder) throws Exception {
//		managerOrder.setOrderNo(CreateOrderUtil.createOrderCode());
		managerOrder.setStatus(Constants.UNAUDIT);
		int ret = iManagerOrderMapper.insert(managerOrder);
		if (ret != 1) {
			throw new Exception("新增进货订单失败");
		}

		// 获取进货订单id
		long managerOrderId = managerOrder.getId();

		// 更新订货单
		long totalMoney = 0;
		String productNameDetail = "";
		for (PhPurchaseList purchase : managerOrder.getPurchaseId()) {
			totalMoney = purchase.getNum() * purchase.getPrice();
			iManagerOrderMapper.updOrderNumAndMoney(purchase.getNum(), totalMoney,
					purchase.getId(), managerOrderId);
		}

		PhPurchaseList purchase1 = managerOrder.getPurchaseId().get(0);
		productNameDetail = purchase1.getProductName() + "等" + purchase1.getNum() + "件";
		// 更新订单商品显示名称
		iManagerOrderMapper.updateManagerOrderProductName(productNameDetail, managerOrderId);

		return ret;
	}

	/**
	 * 
	 * @Title: getManagerOrders
	 * @Description:查询进货订单列表
	 * @author WQiang
	 * @date 2017年3月20日 下午7:10:20
	 * @return
	 */
	@Override
	public Result managerOrdersList(int pageNum, int pageSize, int roleCode, long userId) {
		ManagerOrderDTO managerOrderDTO = new ManagerOrderDTO();
		if (roleCode == RoleEnum.CITY_AGENT.getCode()
				|| roleCode == RoleEnum.COMMUNITY_AGENT.getCode()
				|| roleCode == RoleEnum.COUNTY_AGENT.getCode()) {
			managerOrderDTO.setType(1);
			managerOrderDTO.setUserId(userId);
		} else if(roleCode == RoleEnum.SUPPLIER.getCode()) {
			managerOrderDTO.setType(2);
			managerOrderDTO.setUserId(userId);
		} else {
			return getResult(ResultEnum.USER_ERROR);
		}

		PageHelper.startPage(pageNum, pageSize);
		List<QueryManagerOrderVo> list = iManagerOrderMapper.queryManagerOrders(managerOrderDTO);
		PageInfo<QueryManagerOrderVo> pageInfo = new PageInfo<QueryManagerOrderVo>(list);
		return getResult(ResultEnum.SUCCESS, pageInfo.getList());
	}
}
